package com.shxzz.easy12306.services.order.mq.producer;

import com.shxzz.easy12306.framework.starter.common.mq.constant.OrderRocketMQConstant;
import com.shxzz.easy12306.services.order.mq.base.AbstractCommonProduceSendTemplate;
import com.shxzz.easy12306.framework.starter.common.mq.base.BaseSendExtendDTO;
import com.shxzz.easy12306.framework.starter.common.mq.base.MessageWrapper;
import com.shxzz.easy12306.framework.starter.common.mq.event.DelayCloseOrderEvent;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * @author shxzz
 * @create 2024-03-10-19:35
 */
@Component
public class DelayCloseOrderProducer extends AbstractCommonProduceSendTemplate<DelayCloseOrderEvent> {
    public DelayCloseOrderProducer(RocketMQTemplate rocketMQTemplate) {
        super(rocketMQTemplate);
    }

    @Override
    protected BaseSendExtendDTO buildBaseSendExtendDTO(DelayCloseOrderEvent messageSendEvent) {
        return BaseSendExtendDTO.builder()
                .eventName("延迟关闭订单")
                .topic(OrderRocketMQConstant.DELAY_CLOSE_ORDER_TOPIC)
                .tag(OrderRocketMQConstant.DELAY_CLOSE_ORDER_TAG)
                .keys(messageSendEvent.getOrderSn())
                .sendTimeout(2000L)
                .delayLevel(OrderRocketMQConstant.DELAY_10_MIN_MESSAGE_LEVEL)
                .build();
    }

    @Override
    protected Message<?> buildMessage(DelayCloseOrderEvent messageSendEvent, BaseSendExtendDTO baseSendExtendDTO) {
        return MessageBuilder
                .withPayload(new MessageWrapper<>(baseSendExtendDTO.getKeys(), messageSendEvent))
                .setHeader(MessageConst.PROPERTY_KEYS, baseSendExtendDTO.getKeys())
                .setHeader(MessageConst.PROPERTY_TAGS, baseSendExtendDTO.getTag()).build();
    }
}
